package com.talkweb.historylib.dialog;

import com.talkweb.historylib.ResourceTable;
import com.talkweb.securitycamera.common.base.utils.ResourceHelper;
import ohos.agp.components.*;
import ohos.agp.utils.TextTool;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * 主要用于底部 带输入  双按钮弹窗
 * 对外提供设置title
 * 对外提供取消/确认按钮操作
 * 暴露按钮回调
 */
public class BottomInputDialog extends BaseCommonDialog {
    public static final int RADIUS_VP_20 = 20;
    public static final int MARGIN_VP_20 = 20;
    private Component component;
    private Text tvTitle;
    private TextField tfContent;
    private Button btnCancel;
    private Button btnConfirm;

    private OnClickListener listener;

    public BottomInputDialog(Context context) {
        super(context);
    }

    @Override
    protected boolean isBottomDialog() {
        return false;
    }


    protected void initialize(Context context) {
        super.initialize(context);
        setCornerRadius(AttrHelper.vp2px(RADIUS_VP_20, context));
        setSize(getDialogWidth(MARGIN_VP_20), ComponentContainer.LayoutConfig.MATCH_CONTENT);
        component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_buttom_input_layout, null, false);
        setContentCustomComponent(component);
        initView();
    }

    private void initView() {
        tfContent = (TextField) component.findComponentById(ResourceTable.Id_tf_search);
        tvTitle = (Text) component.findComponentById(ResourceTable.Id_tv_dialog_title);
        btnConfirm = (Button) component.findComponentById(ResourceTable.Id_dialog_confirm);
        btnCancel = (Button) component.findComponentById(ResourceTable.Id_dialog_cancel);
        btnConfirm.setClickedListener(component -> {
            if (!TextTool.isNullOrEmpty(tfContent.getText())) {
                hide();
                if (listener != null) {
                    listener.confirmClick(component, getContentText());
                }
            } else {
                new ToastDialog(getContext()).setText(ResourceHelper.getString(getContext(), ResourceTable.String_input_file_name)).setDuration(2000).show();
            }
        });
        btnCancel.setClickedListener(component -> {
            hide();
            if (listener != null) {
                listener.cancelClick(component);
            }
        });
    }

    /**
     * 提供外部设置title
     *
     * @param title
     */
    public final void setTvTitle(String title) {
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    /**
     * 提供外部设置hint
     *
     * @param hint
     */
    public final void setContentHint(String hint) {
        if (tfContent != null) {
            tfContent.setHint(hint);
        }
    }

    /**
     * 提供外部设置确认按钮文字
     *
     * @param confirmText
     */
    public final void setConfirmText(String confirmText) {
        if (btnConfirm != null) {
            btnConfirm.setText(confirmText);
        }
    }

    /**
     * 提供外部设置取消按钮文字
     *
     * @param cancelText
     */
    public final void setCancelText(String cancelText) {
        if (btnCancel != null) {
            btnCancel.setText(cancelText);
        }
    }

    /**
     * 提供外部获取tf文字
     *
     * @return
     */
    public final String getContentText() {
        return tfContent != null ? tfContent.getText() : "";
    }

    /**
     * 设置点击回调
     *
     * @param listener
     */
    public final void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public interface OnClickListener {

        void cancelClick(Component component);

        void confirmClick(Component component, String content);
    }

}
